﻿namespace Nello.Web.Controls
{
    using Nello;
    using Nello.Web;
    using System;
    using System.ComponentModel;
    using System.Web.UI;
    using System.Web.UI.WebControls;

    [DefaultProperty("Text"), ToolboxData("<{0}:NCalendar runat=server></{0}:NCalendar>")]
    public class NCalendar : TextBox, IDataControl, IPostBackEventHandler
    {
        private bool backToClient = false;
        private string clientArgument = "";
        private bool isignore = false;

        protected override void CreateChildControls()
        {
            this.Controls.Add(new LinkButton());
            base.CreateChildControls();
        }

        public void RaisePostBackEvent(string eventArgument)
        {
            string[] args = eventArgument.Split("|".ToCharArray());
            this.Text = args[0];
            if (this.backToClient)
            {
                string js = "<script type=\"text/javascript\">NCalendar_RestoreUI(\"" + this.ClientID + "\",\"" + args[1] + "\");</script>";
                this.Page.ClientScript.RegisterStartupScript(typeof(string), Nello.Util.NewGuid(), js);
            }
        }

        protected override void Render(HtmlTextWriter output)
        {
            output.AddAttribute(HtmlTextWriterAttribute.Id, this.ID);
            output.AddAttribute(HtmlTextWriterAttribute.Name, this.ID);
            if (this.CssClass != "")
            {
                output.AddAttribute(HtmlTextWriterAttribute.Class, this.CssClass);
            }
            if (!string.IsNullOrEmpty(this.Text))
            {
                output.AddAttribute(HtmlTextWriterAttribute.Value, this.Text);
            }
            output.AddAttribute(HtmlTextWriterAttribute.Style, "cursor:hand");
            output.AddAttribute(HtmlTextWriterAttribute.Type, "text");
            output.AddAttribute(HtmlTextWriterAttribute.ReadOnly, "true");
            output.AddAttribute(HtmlTextWriterAttribute.Onclick, "javascript:this.focus()");
            output.AddAttribute("onFocus", this.ClientID + "_fPopCalendar(this,this," + this.ClientID + "_PopCal); return false;");
            output.RenderBeginTag(HtmlTextWriterTag.Input);
            output.RenderEndTag();
            if (!this.Page.ClientScript.IsClientScriptBlockRegistered("clientScript"))
            {
                output.WriteLine();
                output.WriteLine("<script type=\"text/javascript\">");
                output.WriteLine("var " + this.ClientID + "_gdCtrl = new Object();");
                output.WriteLine("var " + this.ClientID + "_gcGray = \"#808080\";");
                output.WriteLine("var " + this.ClientID + "_gcToggle = \"highlight\";");
                output.WriteLine("var " + this.ClientID + "_gcBG = \"threedface\";");
                output.WriteLine("var " + this.ClientID + "_gMonths = new Array(\"一月\",\"二月\",\"三月\",\"四月\",\"五月\",\"六月\",\"七月\",\"八月\",\"九月\",\"十月\",\"十一月\",\"十二月\");");
                output.WriteLine("var " + this.ClientID + "_gdCurDate = new Date();");
                output.WriteLine("var " + this.ClientID + "_giYear = " + this.ClientID + "_gdCurDate.getFullYear();");
                output.WriteLine("var " + this.ClientID + "_giMonth = " + this.ClientID + "_gdCurDate.getMonth()+1;");
                output.WriteLine("var " + this.ClientID + "_giDay = " + this.ClientID + "_gdCurDate.getDate();");
                output.WriteLine("var " + this.ClientID + "_sxYear = " + this.ClientID + "_giYear;");
                output.WriteLine("var " + this.ClientID + "_sxMonth = " + this.ClientID + "_giMonth;");
                output.WriteLine("var " + this.ClientID + "_sxDay = " + this.ClientID + "_giDay;");
                output.WriteLine("var " + this.ClientID + "_sxDatestr = " + this.ClientID + "_gdCtrl.value;");
                output.WriteLine("if (" + this.ClientID + "_sxDatestr != \"\"){");
                output.WriteLine("var " + this.ClientID + "_sxDate = new Date(" + this.ClientID + "_sxDatestr);");
                output.WriteLine(this.ClientID + "_sxYear = " + this.ClientID + "_sxDate.getFullYear();");
                output.WriteLine("}");
                output.WriteLine("var " + this.ClientID + "_VicPopCal = new Object();");
                output.WriteLine("function " + this.ClientID + "_mouseover(obj){");
                output.WriteLine("obj.style.borderTop = 'buttonshadow 1px solid';");
                output.WriteLine("obj.style.borderLeft = 'buttonshadow 1px solid';");
                output.WriteLine("obj.style.borderRight = 'buttonhighlight 1px solid';");
                output.WriteLine("obj.style.borderBottom = 'buttonhighlight 1px solid';");
                output.WriteLine("}");
                output.WriteLine("function " + this.ClientID + "_mouseout(obj){");
                output.WriteLine("obj.style.borderTop = 'buttonhighlight 1px solid';");
                output.WriteLine("obj.style.borderLeft = 'buttonhighlight 1px solid';");
                output.WriteLine("obj.style.borderRight = 'buttonshadow 1px solid';");
                output.WriteLine("obj.style.borderBottom = 'buttonshadow 1px solid';");
                output.WriteLine("}");
                output.WriteLine("function " + this.ClientID + "_mousedown(obj){");
                output.WriteLine("obj.style.borderTop = 'buttonshadow 1px solid';");
                output.WriteLine("obj.style.borderLeft = 'buttonshadow 1px solid';");
                output.WriteLine("obj.style.borderRight = 'buttonhighlight 1px solid';");
                output.WriteLine("obj.style.borderBottom = 'buttonhighlight 1px solid';");
                output.WriteLine("}");
                output.WriteLine("function " + this.ClientID + "_mouseup(obj){");
                output.WriteLine("obj.style.borderTop = 'buttonhighlight 1px solid';");
                output.WriteLine("obj.style.borderLeft = 'buttonhighlight 1px solid';");
                output.WriteLine("obj.style.borderRight = 'buttonshadow 1px solid';");
                output.WriteLine("obj.style.borderBottom = 'buttonshadow 1px solid';");
                output.WriteLine("}");
                output.WriteLine("function " + this.ClientID + "_fPopCalendar(popCtrl, dateCtrl, popCal){");
                output.WriteLine("if(parent.event){parent.event.cancelBubble=true;}");
                output.WriteLine(this.ClientID + "_VicPopCal = popCal;");
                output.WriteLine(this.ClientID + "_gdCtrl = dateCtrl;");
                output.WriteLine(this.ClientID + "_fSetYearMon(" + this.ClientID + "_giYear, " + this.ClientID + "_giMonth);");
                output.WriteLine("var point = " + this.ClientID + "_fGetXY(popCtrl);");
                output.WriteLine("with (" + this.ClientID + "_VicPopCal.style) {left = point.x;top  = point.y+popCtrl.offsetHeight+1;visibility = 'visible';}");
                output.WriteLine(this.ClientID + "_VicPopCal.focus();");
                output.WriteLine("}");
                output.WriteLine("function " + this.ClientID + "_fSetDate(iYear, iMonth, iDay){");
                output.WriteLine("if ((iYear == 0) && (iMonth == 0) && (iDay == 0)){");
                output.WriteLine(this.ClientID + "_gdCtrl.value = \"\";");
                output.WriteLine("}");
                output.WriteLine("else{");
                output.WriteLine("if (iMonth < 10){iMonth = \"0\"+iMonth;}");
                output.WriteLine("if (iDay < 10){iDay = \"0\"+iDay;}");
                output.WriteLine(this.ClientID + "_gdCtrl.value = iYear+\"-\"+iMonth+\"-\"+iDay;");
                output.WriteLine("__doPostBack('" + this.ClientID + "'," + this.ClientID + "_gdCtrl.value+'|'+'" + this.clientArgument + "')");
                output.WriteLine("}");
                output.WriteLine(this.ClientID + "_VicPopCal.style.visibility = \"hidden\";");
                output.WriteLine("}");
                output.WriteLine("function " + this.ClientID + "_fSetSelected(aCell){");
                output.WriteLine("var iOffset = 0;");
                output.WriteLine("var iYear = parseInt(document.all." + this.ClientID + "_tbSelYear.value);");
                output.WriteLine("var iMonth = parseInt(document.all." + this.ClientID + "_tbSelMonth.value);");
                output.WriteLine("aCell.bgColor = " + this.ClientID + "_gcBG;");
                output.WriteLine("with (aCell.children[\"" + this.ClientID + "_cellText\"]){");
                output.WriteLine("var iDay = parseInt(innerText);");
                output.WriteLine("if (color==" + this.ClientID + "_gcGray){iOffset = (Victor<10)?-1:1;}");
                output.WriteLine("iMonth += iOffset;");
                output.WriteLine("if (iMonth<1) {\tiYear--; iMonth = 12;}else{if (iMonth>12){iYear++;iMonth = 1;}}");
                output.WriteLine("}");
                output.WriteLine(this.ClientID + "_fSetDate(iYear, iMonth, iDay);");
                output.WriteLine("}");
                output.WriteLine("function " + this.ClientID + "_Point(iX, iY){this.x = iX;this.y = iY;}");
                output.WriteLine("function " + this.ClientID + "_fBuildCal(iYear, iMonth){");
                output.WriteLine("var aMonth=new Array();");
                output.WriteLine("for(i=1;i<7;i++){aMonth[i]=new Array(i);}");
                output.WriteLine("var dCalDate=new Date(iYear, iMonth-1, 1);");
                output.WriteLine("var iDayOfFirst=dCalDate.getDay();");
                output.WriteLine("var iDaysInMonth=new Date(iYear, iMonth, 0).getDate();");
                output.WriteLine("var iOffsetLast=new Date(iYear, iMonth-1, 0).getDate()-iDayOfFirst+1;");
                output.WriteLine("var iDate = 1;");
                output.WriteLine("var iNext = 1;");
                output.WriteLine("for (d = 0; d < 7; d++){aMonth[1][d] = (d<iDayOfFirst)?-(iOffsetLast+d):iDate++;}");
                output.WriteLine("for (w = 2; w < 7; w++){for (d = 0; d < 7; d++){aMonth[w][d] = (iDate<=iDaysInMonth)?iDate++:-(iNext++);}}");
                output.WriteLine("return aMonth;");
                output.WriteLine("}");
                output.WriteLine("function " + this.ClientID + "_fDrawCal(iYear, iMonth, iDay, iCellWidth, iDateTextSize) {");
                output.WriteLine("var WeekDay = new Array(\"日\",\"一\",\"二\",\"三\",\"四\",\"五\",\"六\");");
                output.WriteLine("var styleTD = \" bgcolor='\"+" + this.ClientID + "_gcBG+\"' width='\"+iCellWidth+\"' bordercolor='\"+" + this.ClientID + "_gcBG+\"' valign='middle' align='center' style='font-size: 12px;background: buttonface;border-top: buttonhighlight 1px solid;border-left: buttonhighlight 1px solid;border-right: buttonshadow 1px solid;\tborder-bottom: buttonshadow 1px solid;\";");
                output.WriteLine("with (document) {");
                output.WriteLine("write(\"<tr align='center'>\");");
                output.WriteLine("for(i=0; i<7; i++){write(\"<td height='20' \"+styleTD+\"color:#990099' >\" + WeekDay[i] + \"</td>\");}");
                output.WriteLine("write(\"</tr>\");");
                output.WriteLine("for (w = 1; w < 7; w++) {");
                output.WriteLine("write(\"<tr align='center'>\");");
                output.WriteLine("for (d = 0; d < 7; d++) {");
                output.WriteLine("write(\"<td width='10%' height='15' id=" + this.ClientID + "_calCell \"+styleTD+\"cursor:hand;' onmouseover='" + this.ClientID + "_mouseover(this)' onmouseout='" + this.ClientID + "_mouseout(this)' onmousedown='" + this.ClientID + "_mousedown(this)' onmouseup='" + this.ClientID + "_mouseup(this)' onclick='" + this.ClientID + "_fSetSelected(this)'>\");");
                output.WriteLine("write(\"<font style='font-size: 13px;' id=" + this.ClientID + "_cellText Victor='Liming Weng'> </font>\");");
                output.WriteLine("write(\"</td>\");");
                output.WriteLine("}");
                output.WriteLine("write(\"</tr>\");");
                output.WriteLine("}");
                output.WriteLine("}");
                output.WriteLine("}");
                output.WriteLine("function " + this.ClientID + "_fUpdateCal(iYear, iMonth) {");
                output.WriteLine(this.ClientID + "_sxYear = iYear;");
                output.WriteLine(this.ClientID + "_sxMonth = iMonth;");
                output.WriteLine(this.ClientID + "_yeartd1.innerText = " + this.ClientID + "_sxYear + \"年\";");
                output.WriteLine(this.ClientID + "_monthtd1.innerText = " + this.ClientID + "_gMonths[" + this.ClientID + "_sxMonth-1];");
                output.WriteLine(this.ClientID + "_myMonth = " + this.ClientID + "_fBuildCal(iYear, iMonth);");
                output.WriteLine("var i = 0;");
                output.WriteLine("for (w = 0; w < 6; w++){");
                output.WriteLine("for (d = 0; d < 7; d++){");
                output.WriteLine("with (" + this.ClientID + "_cellText[(7*w)+d]) {");
                output.WriteLine("Victor = i++;");
                output.WriteLine("if (" + this.ClientID + "_myMonth[w+1][d]<0) {");
                output.WriteLine("color = " + this.ClientID + "_gcGray;");
                output.WriteLine("innerText = -" + this.ClientID + "_myMonth[w+1][d];");
                output.WriteLine("}else{");
                output.WriteLine("color = ((d==0)||(d==6))?\"red\":\"black\";");
                output.WriteLine("innerText = " + this.ClientID + "_myMonth[w+1][d];");
                output.WriteLine("}");
                output.WriteLine("}");
                output.WriteLine("}");
                output.WriteLine("}");
                output.WriteLine("}");
                output.WriteLine("function " + this.ClientID + "_fSetYearMon(iYear, iMon){");
                output.WriteLine(this.ClientID + "_sxYear = iYear;");
                output.WriteLine(this.ClientID + "_sxMonth = iMon;");
                output.WriteLine(this.ClientID + "_yeartd1.innerText = " + this.ClientID + "_sxYear + \"年\";");
                output.WriteLine(this.ClientID + "_monthtd1.innerText = " + this.ClientID + "_gMonths[" + this.ClientID + "_sxMonth-1];");
                output.WriteLine("document.all." + this.ClientID + "_tbSelMonth.options[iMon-1].selected = true;");
                output.WriteLine("for (i = 0; i < document.all." + this.ClientID + "_tbSelYear.length; i++){");
                output.WriteLine("if (document.all." + this.ClientID + "_tbSelYear.options[i].value == iYear){");
                output.WriteLine("document.all." + this.ClientID + "_tbSelYear.options[i].selected = true;");
                output.WriteLine("}");
                output.WriteLine("}");
                output.WriteLine(this.ClientID + "_fUpdateCal(iYear, iMon);");
                output.WriteLine("}");
                output.WriteLine("function " + this.ClientID + "_fPrevMonth(){");
                output.WriteLine("var iMon = document.all." + this.ClientID + "_tbSelMonth.value;");
                output.WriteLine("var iYear = document.all." + this.ClientID + "_tbSelYear.value;");
                output.WriteLine("if (--iMon<1) {");
                output.WriteLine("iMon = 12;");
                output.WriteLine("iYear--;");
                output.WriteLine("}");
                output.WriteLine(this.ClientID + "_fSetYearMon(iYear, iMon);");
                output.WriteLine("}");
                output.WriteLine("function " + this.ClientID + "_fNextMonth(){");
                output.WriteLine("var iMon = document.all." + this.ClientID + "_tbSelMonth.value;");
                output.WriteLine("var iYear = document.all." + this.ClientID + "_tbSelYear.value;");
                output.WriteLine("if (++iMon>12) {");
                output.WriteLine("iMon = 1;");
                output.WriteLine("iYear++;");
                output.WriteLine("}");
                output.WriteLine(this.ClientID + "_fSetYearMon(iYear, iMon);");
                output.WriteLine("}");
                output.WriteLine("function " + this.ClientID + "_fGetXY(aTag){");
                output.WriteLine("var oTmp = aTag;");
                output.WriteLine("var pt = new " + this.ClientID + "_Point(0,0);");
                output.WriteLine("do {");
                output.WriteLine("pt.x += oTmp.offsetLeft;");
                output.WriteLine("pt.y += oTmp.offsetTop;");
                output.WriteLine("oTmp = oTmp.offsetParent;");
                output.WriteLine("} while(oTmp.tagName!=\"BODY\");");
                output.WriteLine("return pt;");
                output.WriteLine("}");
                output.WriteLine("with (document){");
                output.WriteLine("write(\"<Div id='" + this.ClientID + "_PopCal' onclick='event.cancelBubble=true' style='POSITION:absolute; VISIBILITY: hidden; bordercolor:#000000;border:2px ridge;width:10;z-index:100;'>\");");
                output.WriteLine("write(\"<iframe frameBorder=0 width=180 scrolling=no height=170></iframe>\")");
                output.WriteLine("write(\"<table id='" + this.ClientID + "_popTable' border='1' bgcolor='#eeede8' cellpadding='0' cellspacing='0' style='font-size:12px;Z-INDEX:202;position:absolute;top:0;left:0;'>\");");
                output.WriteLine("write(\"<TR>\");");
                output.WriteLine("write(\"<td valign='middle' align='center' style='cursor:default'>\");");
                output.WriteLine("write(\"<table width='176' border='0' cellpadding='0' cellspacing='0'>\");");
                output.WriteLine("write(\"<tr align='center'>\");");
                output.WriteLine("write(\"<td height='22' width='20' name='" + this.ClientID + "_PrevMonth' style='font-family:\\\"webdings\\\";font-size:15px' onClick='" + this.ClientID + "_fPrevMonth()' onmouseover='this.style.color=\\\"#ff9900\\\"' onmouseout='this.style.color=\\\"\\\"'>3</td>\");");
                output.WriteLine("write(\"<td width='64' id='" + this.ClientID + "_yeartd1' style='font-size:12px;display:none' onmouseover='" + this.ClientID + "_yeartd1.style.display=\\\"none\\\";" + this.ClientID + "_yeartd2.style.display=\\\"\\\";' onmouseout='this.style.background=\\\"\\\"'>\");");
                output.WriteLine("write(" + this.ClientID + "_sxYear + \"年\");");
                output.WriteLine("write(\"</td>\");");
                output.WriteLine("write(\"<td width='64' id='" + this.ClientID + "_yeartd2' style='' onmouseout=''>\");");
                output.WriteLine("write(\"<SELECT style='width:64px;font-size: 12px;font-family: 宋体;' id='" + this.ClientID + "_tbSelYear' onChange='" + this.ClientID + "_fUpdateCal(document.all." + this.ClientID + "_tbSelYear.value, document.all." + this.ClientID + "_tbSelMonth.value);" + this.ClientID + "_yeartd2.style.display=\\\"\\\";" + this.ClientID + "_yeartd1.style.display=\\\"none\\\";' Victor='Won'>\");");
                output.WriteLine("for(i=1930;i<2015;i++){");
                output.WriteLine("write(\"<OPTION value='\"+i+\"'>\"+i+\"年</OPTION>\");");
                output.WriteLine("}");
                output.WriteLine("write(\"</SELECT>\");");
                output.WriteLine("write(\"</td>\");");
                output.WriteLine("write(\"<td width='72' id='" + this.ClientID + "_monthtd1' style='font-size:12px;display:none' onmouseover='" + this.ClientID + "_monthtd1.style.display=\\\"none\\\";" + this.ClientID + "_monthtd2.style.display=\\\"\\\";' onmouseout='this.style.background=\\\"\\\"'>\");");
                output.WriteLine("write(" + this.ClientID + "_gMonths[" + this.ClientID + "_sxMonth-1]);");
                output.WriteLine("write(\"</td>\");");
                output.WriteLine("write(\"<td width='72' id='" + this.ClientID + "_monthtd2' style='' onmouseout=''>\");");
                output.WriteLine("write(\"<select style='width:72px;font-size: 12px;font-family: 宋体;' id='" + this.ClientID + "_tbSelMonth' onChange='" + this.ClientID + "_fUpdateCal(document.all." + this.ClientID + "_tbSelYear.value, document.all." + this.ClientID + "_tbSelMonth.value);" + this.ClientID + "_monthtd2.style.display=\\\"\\\";" + this.ClientID + "_monthtd1.style.display=\\\"none\\\";' Victor='Won'>\");");
                output.WriteLine("for (i=0; i<12; i++){");
                output.WriteLine("write(\"<option value='\"+(i+1)+\"'>\"+" + this.ClientID + "_gMonths[i]+\"</option>\");");
                output.WriteLine("}");
                output.WriteLine("write(\"</SELECT>\");");
                output.WriteLine("write(\"</td>\");");
                output.WriteLine("write(\"<td width='20' name='" + this.ClientID + "_PrevMonth' style='font-family:\\\"webdings\\\";font-size:15px' onclick='" + this.ClientID + "_fNextMonth()' onmouseover='this.style.color=\\\"#ff9900\\\"' onmouseout='this.style.color=\\\"\\\"'>4</td>\");");
                output.WriteLine("write(\"</tr>\");");
                output.WriteLine("write(\"</table>\");");
                output.WriteLine("write(\"</td></TR><TR><td align='center'>\");");
                output.WriteLine("write(\"<DIV style='background-color:teal;'><table width='100%' border='0' bgcolor='threedface' cellpadding='0' cellspacing='0'>\");");
                output.WriteLine(this.ClientID + "_fDrawCal(" + this.ClientID + "_giYear, " + this.ClientID + "_giMonth, " + this.ClientID + "_giDay, 19, 14);");
                output.WriteLine("write(\"</table></DIV>\");");
                output.WriteLine("write(\"</td></TR><TR><TD height='20' align='center' valign='bottom'>\");");
                output.WriteLine("write(\"<font style='cursor:hand;font-size:12px' onclick='" + this.ClientID + "_fSetDate(0,0,0)' onMouseOver='this.style.color=\\\"#0033FF\\\"' onMouseOut='this.style.color=0'>清空</font>\");");
                output.WriteLine("write(\"&nbsp;&nbsp;&nbsp;&nbsp;\");");
                output.WriteLine("write(\"<font style='cursor:hand;font-size:12px' onclick='" + this.ClientID + "_fSetDate(" + this.ClientID + "_giYear," + this.ClientID + "_giMonth," + this.ClientID + "_giDay)' onMouseOver='this.style.color=\\\"#0033FF\\\"' onMouseOut='this.style.color=0'>今天: \"+" + this.ClientID + "_giYear+\"-\"+" + this.ClientID + "_giMonth+\"-\"+" + this.ClientID + "_giDay+\"</font>\");");
                output.WriteLine("write(\"</TD></TR></TD></TR></TABLE>\");");
                output.WriteLine("write(\"</Div>\");");
                output.WriteLine("}");
                output.WriteLine("</script>");
                output.WriteLine("<SCRIPT event=onclick() for=document>" + this.ClientID + "_PopCal.style.visibility = 'hidden';</SCRIPT>");
                this.Page.ClientScript.RegisterClientScriptBlock(typeof(string), "clientScript", "");
            }
        }

        [Browsable(true), Category("Nello"), Description("是否发送调用函数及参数至客户端")]
        public bool BackToClient
        {
            get
            {
                return this.backToClient;
            }
            set
            {
                this.backToClient = value;
            }
        }

        [Category("Nello"), Browsable(true), Description("服务器端传递至客户端的参数")]
        public string ClientArgument
        {
            get
            {
                return this.clientArgument;
            }
            set
            {
                this.clientArgument = value;
            }
        }

        [Browsable(false), Category("Nello"), Description("此控件的数据类型")]
        public Nello.DataType DataType
        {
            get
            {
                return Nello.DataType.DateTime;
            }
            set
            {
            }
        }

        [Description("获取或设置此控件的值"), Category("Nello"), Browsable(false)]
        public object DataValue
        {
            get
            {
                return DataTypeUtil.GetValue(Nello.DataType.DateTime, this.Text);
            }
            set
            {
                if (value == null)
                {
                    this.Text = "";
                }
                else
                {
                    this.Text = value.ToString();
                }
            }
        }

        [Browsable(true), Category("Nello"), Description("是否被框架忽略并作为普通控件使用")]
        public bool IsIgnore
        {
            get
            {
                return this.isignore;
            }
            set
            {
                this.isignore = value;
            }
        }
    }
}

